//
//  DestinationViewController.m
//  PTravel
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 李涵. All rights reserved.
//

#import "DestinationViewController.h"

@interface DestinationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UITableViewDataSource, UITableViewDelegate>
@property (retain, nonatomic)UICollectionView *myCollection;
@property (retain, nonatomic)UITableView *myTableView;
@property (retain, nonatomic)NSMutableArray *hotCountryArr;//热点城市数组
@property (retain, nonatomic)NSMutableArray *countryArr;//城市数组

@property (retain, nonatomic)UIView *bgView;

@property (retain, nonatomic)MBProgressHUD *hub;


@end

@implementation DestinationViewController

- (void)dealloc
{
    [_myCollection release];
    [_myTableView release];
    [_hotCountryArr release];
    [_countryArr release];
    [_bgView release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.contnientId = 10;
    self.navigationController.navigationBar.translucent = YES;
    UIImage *image = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:image];
    self.automaticallyAdjustsScrollViewInsets = NO;
    

    NSArray *temp = self.tabBarController.view.subviews;
    UIView *view = temp[2];
    view.frame = CGRectMake(0, self.view.bounds.size.height-49, self.view.bounds.size.width, self.view.frame.size.height);
    
    //夜间模式判断
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeColor:) name:@"night" object:nil];
}

- (void)changeColor:(NSNotificationCenter *)notification
{
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:TEXTCOLOR};
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.myCollection.backgroundColor = BGCOLOR;
    [self.myCollection reloadData];
    self.myTableView.backgroundColor = BGCOLOR;
    [self.myTableView reloadData];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.barTintColor = BARCOLOR;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:TEXTCOLOR};
    [self getCreateView];
    
    //取消图片渲染
    UIImage *image = [UIImage imageNamed:@"iconfont-mulu.png"];
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftMenu:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blueColor];
    self.navigationItem.leftBarButtonItem.width = 18;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    self.myCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, BOUNDS.size.width, BOUNDS.size.height-106) collectionViewLayout:layout];
    self.myCollection.backgroundColor = BGCOLOR;
    self.myCollection.delegate = self;
    self.myCollection.dataSource = self;
    [_myCollection release];
    [layout release];
    
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    [self.myCollection registerClass:[HotCountryCollectionViewCell class] forCellWithReuseIdentifier:@"HotCountryCollectionViewCell"];
    [self.myCollection registerClass:[CountryCollectionViewCell class] forCellWithReuseIdentifier:@"countryCollectionViewCell"];

    
    //轻扫手势
    UISwipeGestureRecognizer *swipeRight1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRight:)];
    swipeRight1.direction = UISwipeGestureRecognizerDirectionRight;
    UISwipeGestureRecognizer *swipeLeft1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)];
    swipeLeft1.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.myCollection addGestureRecognizer:swipeRight1];
    [swipeRight1 release];
    [self.myCollection addGestureRecognizer:swipeLeft1];
    [swipeLeft1 release];
    [self.view addSubview:self.myCollection];
    [_myCollection release];
    [layout release];
    
    self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, 75, BOUNDS.size.height)];
    self.bgView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.bgView];
    [_bgView release];
    [UIView animateWithDuration:2 animations:^{
        
        self.bgView.frame = CGRectMake(-75, 64, 75, BOUNDS.size.height);
    }];
    
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 75, BOUNDS.size.height) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    self.myTableView.backgroundColor = BGCOLOR;
    [self.myTableView registerClass:[ContinentTableViewCell class] forCellReuseIdentifier:@"ContinentTableViewCell"];
    [_myTableView release];
    
    
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.bgView addSubview:self.myTableView];
}
#pragma mark ** 点击目录
- (void)leftMenu:(UIBarButtonItem *)barButton
{
    
    if (self.bgView.frame.origin.x == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(-75, 64, 75, self.view.frame.size.height);
        }];
    }
    else
    {
        [UIView animateWithDuration:0.5 animations:^{
            self.bgView.frame = CGRectMake(0, 64, 75, self.view.frame.size.height);
        }];
    }
    
    
}

#pragma  mark ** 左滑显示目录

- (void)swipeLeft:(UISwipeGestureRecognizer *)swipe
{
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(-75, 64, 75, self.view.frame.size.height);
    }];
    
}

#pragma  mark ** 右滑回收目录

- (void)swipeRight:(UISwipeGestureRecognizer *)swipe
{
    [UIView animateWithDuration:0.5 animations:^{
        self.bgView.frame = CGRectMake(0, 64, 75, self.view.frame.size.height);

    }];
    
}

#pragma mark === tableView点击触发该方法
- (void)getCreateView
{
    self.continentArr = [NSMutableArray array];
    self.hotCountryArr = [NSMutableArray array];
    self.countryArr = [NSMutableArray array];
    self.navigationItem.title = @"世界如此之大";
    
    
    [self getData];
    
}

#pragma mark === 解析网络数据
- (void)getData
{
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"Waiting···";
    [AppTools getAFN:@"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_android&client_secret=9fcaae8aefc4f9ac4915&v=1&track_deviceid=866963025209810&track_app_version=6.5.5&track_app_channel=xiaomi&track_device_info=cancro&track_os=Android4.4.4&app_installtime=1439787107899&lat=38.883246&lon=121.544911" block:^(id result) {
        NSMutableDictionary *bigDic = result;
        NSMutableDictionary *dataArr = [bigDic objectForKey:@"data"];
        for (NSMutableDictionary *dic in dataArr) {
            Continent *continent = [[Continent alloc] init];
            [continent setValuesForKeysWithDictionary:dic];
            [self.continentArr addObject:continent];
            
            if (self.contnientId == continent.id) {
                NSMutableArray *arr2 = [dic objectForKey:@"hot_country"];
                for (NSMutableDictionary *dic2 in arr2) {
                    Country *country = [[Country alloc] init];
                    [country setValuesForKeysWithDictionary:dic2];
                    [self.hotCountryArr addObject:country];
                }
            }
            if (self.contnientId == continent.id) {
                NSMutableArray *arr3 = [dic objectForKey:@"country"];
                for (NSMutableDictionary *dic3 in arr3) {
                    Country *country = [[Country alloc] init];
                    [country setValuesForKeysWithDictionary:dic3];
                    [self.countryArr addObject:country];
                }
            }
        }
        
        [self.myTableView reloadData];
        [self.myCollection reloadData];
        [self.hub hide:YES];
       
        
    }];
}

//左条滑动
- (void)scrollAction
{
    [UIView animateWithDuration:5 animations:^{
        self.bgView.frame = CGRectMake(0, 64, 0, BOUNDS.size.height);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.continentArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContinentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContinentTableViewCell" forIndexPath:indexPath];
    cell.continent = [self.continentArr objectAtIndex:indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    // 取消cell的置灰效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return BOUNDS.size.height/9.5;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Continent *continent = [self.continentArr objectAtIndex:indexPath.row];
    self.contnientId = continent.id;
    [self getCreateView];
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotCountryArr.count+self.countryArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.hotCountryArr.count) {
        HotCountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotCountryCollectionViewCell" forIndexPath:indexPath];
        cell.hotCountry = [self.hotCountryArr objectAtIndex:indexPath.row];
        return cell;
        
    }else{
        CountryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"countryCollectionViewCell" forIndexPath:indexPath];
        
        cell.country = [self.countryArr objectAtIndex:(indexPath.row-self.hotCountryArr.count)];
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row <self.hotCountryArr.count) {
        return CGSizeMake((BOUNDS.size.width-15)/2-15, BOUNDS.size.height/3.2);
    }else{
        return CGSizeMake((BOUNDS.size.width-15)/2-15, 60);
    }
    
}

#pragma mark === collection的点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.hotCountryArr.count) {
        
       Country *country = [self.hotCountryArr objectAtIndex:indexPath.row];
        DetailViewController *detail = [[DetailViewController alloc] init];
        [self.navigationController pushViewController:detail animated:YES];
        detail.cityId = country.id;
        [country release];
        
    }else{
        
        Country *country = [self.countryArr objectAtIndex:(indexPath.row-self.hotCountryArr.count)];
        DetailViewController *detail = [[DetailViewController alloc] init];
        [self.navigationController pushViewController:detail animated:YES];
        detail.cityId = country.id;
        [country release];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
